package nju.mscs.businesslogic;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Iterator;
import nju.mscs.data.object.*;

public class BackStage {
	public static final int pointsLevel1=1000;
	public static final int pointsLevel2=2000;
	public static final int pointsLevel3=5000;
	public static final int pointslift=5000;
	private Database db;
	protected static boolean hasGift;
	protected static ArrayList<VipConsumer> birthGift=new ArrayList<VipConsumer>();
	protected static ArrayList<VipConsumer> pointsGift=new ArrayList<VipConsumer>();
	public BackStage(){
	db=LogIn.db;
	searchVipGift();
	}
	private void searchVipGift(){//检测是否有用户需发礼品
	Calendar ca=Calendar.getInstance();
	Iterator<VipConsumer> it=db.getVipList();
	VipConsumer vip=null;
	while(it.hasNext()){
		vip=it.next();
		Date birth=vip.getBirth();
		Calendar ca1=Calendar.getInstance();
		Calendar ca2=Calendar.getInstance();
		ca1.set(birth.getYear(), birth.getMonth(), birth.getDate());
		if(Math.abs((ca1.getTimeInMillis()-ca2.getTimeInMillis())/(60*1000*60*24))<3&&vip.isBirthGift()==false){
			birthGift.add(vip);
			}
		if(compare(vip.getSumPoints(),vip.getPointGift())){
			pointsGift.add(vip);
		}
		if(birthGift.size()!=0||pointsGift.size()!=0){
			hasGift=true;
			}
		}
	}
	public void dealDatabase(Database im){
		im.getPromotionList();//替换策略
		Iterator<User> iu=im.getUserList();
		while(iu.hasNext()){
			User user=iu.next();
			if(db.searchUserById(user.getId())==null){
				db.addUser(user.getName(), user.getPassword(), user.getId(), user.getType(),user.getAdress());
			}
		}
	}
	private boolean compare(int points, int level){
		if(level<=2){
			if(points>=this.pointsLevel1&&level==0);
			else if(points>=this.pointsLevel2&&level==1);
			else if(points>=this.pointsLevel3&&level==2);
			else
				return false;
		}
		else{
			int i=1;
			for(;;i++){
				if(points<this.pointslift*(i+1))
					break;
			}
			if(points>=this.pointslift*i&&points==i+2)
				return true;
			else
				return false;
		}
		return false;
	}
//public static void main(String[] arges){
//	new BackStage().searchVipGift();
//}
}
